package com.polly.esphwevent.common.utils;

/**
 * @author liqin@nd.com.cn
 * @version V1.0
 * @title: RCThreadLocal
 * @description: TODO(用一句话描述该文件做什么)
 * @date: 2019/3/7 11:07
 */

public class RCThreadLocal {

    static class ResourceClass {

        public final static ThreadLocal<String> r1 = new ThreadLocal<String>();

        public final static ThreadLocal<String> r2 = new ThreadLocal<String>();

    }

    static class InitT1 {

        public void setOne(String value) {
            ResourceClass.r1.set(value);
        }

        public void setTwo(String value) {
            ResourceClass.r2.set(value);
        }
    }

    static class InitT2 {
        public void display() {
            System.out.println(ResourceClass.r1.get()
                    + ":" + ResourceClass.r2.get());
        }
    }


    public static void main(String[] args) {
        final InitT1 a = new InitT1();
        final InitT2 b = new InitT2();
        for(int i = 0 ; i < 15 ; i ++) {
            final String resouce1 = "线程-" + i;
            final String resouce2 = " value = (" + i + ")";
            new Thread() {
                public void run() {
                    try {
                        a.setOne(resouce1);
                        a.setTwo(resouce2);
                        b.display();
                    }finally {
                        ResourceClass.r1.remove();
                        ResourceClass.r2.remove();
                    }
                }
            }.start();
        }
    }
}
